package com.itheima;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
/**
 * 环绕通知前...
 * 前置通知...
 * add user ......
 * 后置通知...6666
 * 最终通知...
 * 环绕通知后...
 */
//加Component才会生效
@Component
@Aspect
@Order(Integer.MAX_VALUE)
public class MyAspect3 {

    /**
     * 前置通知，实现类生效
     */
    @Before("execution(* com.itheima.service.BookServiceImpl.saveBatch(..))")
    public void before() {
        System.out.println("前置通知...");
    }

    /**
     * 后置通知，接口也生效
     * returnVal,切点方法执行后的返回值
     */
    @AfterReturning(value = "execution(* com.itheima.service.BookServiceImpl.saveBatch(..))", returning = "returnVal")
    public void AfterReturning(Object returnVal) {
        System.out.println("后置通知..." + returnVal);
    }

    @Pointcut(value = "execution(* com.itheima.service..*.saveBatch(..))")
    public void around() {
    }

    /**
     * 环绕通知
     *
     * @param joinPoint 可用于执行切点的类
     * @return
     * @throws Throwable
     */
    @Around("around()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("环绕通知前...");
        Object obj = (Object) joinPoint.proceed();
        System.out.println("环绕通知后...");
        return obj;
    }

    /**
     * 抛出通知
     *
     * @param e
     */
    @AfterThrowing(value = "execution(* com.itheima.service..*.saveBatch(..))", throwing = "e")
    public void afterThrowable(Throwable e) {
        System.out.println("出现异常:msg=" + e.getMessage());
    }

    /**
     * 无论什么情况下都会执行的方法
     */
    @After(value = "execution(* com.itheima.service..*.saveBatch(..))")
    public void after() {
        System.out.println("最终通知...");
    }
}

